ActiveAdmin.register RunningAccount do
  filter :order_no, label: '订单号'
  filter :created_at_or_updated_at, label: '日期', as: :date_range
  actions :index, :show


  scope '0 - 课程', :course, default: true
  scope '1 - 活动', :campaign
  scope '2 - 训练营', :camp
  scope '3 - 教练培训', :coach
  scope '4 - 管家', :boss

  index do
    column '第三分流水号', :no
    column '付款账户', :buyer
    column '订单号', :order_no
    column '集团', :company_name
    column '门店', :store_name
    column '产品', :product
    column '应收金额', :total do |ra|
      ra.pay_amount > ra.total ? ra.pay_amount : ra.total
    end
    column '实收金额', :pay_amount
    column '支付方式', :pay_type
    column '状态', :status do |ra|
      if ra.pending?
        status_tag(ra.status_i18n, class: :ok)
      end
      if ra.refunded?
        status_tag(ra.status_i18n, class: :error)
      end
    end
    column '交易时间', :created_at
    column '退款时间' do |ra|
      ra.updated_at.strftime('%Y-%m-%d %H:%M:%S') if ra.refunded?
    end
  end

  controller do

    def index
      if params[:format].eql?("csv")
        csv = CSV.generate(headers: true) do |row|
          row << %w(订单号 第三方流水号 来源 集团 门店 产品 订单金额 支付金额 支付方式 支付时间 财务状态)
          search_start_date = (params[:q][:created_at_or_updated_at_gteq_date] || params[:q][:created_at_or_updated_at_gteq_datetime]).to_date rescue Date.today
          search_end_date = ((params[:q][:created_at_or_updated_at_lteq_date] || params[:q][:created_at_or_updated_at_lteq_datetime]).to_date rescue Date.today) + 1.days
          RunningAccount.ransack(params[:q]).result.each do |t|
            t.total = t.pay_amount if t.pay_type.eql?('苹果')
            order_type = case t.order_type
                         when 'Order'
                           '课程'
                         when 'CoachOrder'
                           '教练'
                         when 'CampOrder'
                           '训练营'
                         when 'Campaign'
                           '活动'
                         when 'Boss::Order'
                           '管家'
                         else
                           '未知'
                         end
            if t.refunded?
              if search_start_date.to_time <= t.created_at && t.updated_at <= search_end_date.to_time
                row << [t.order_no, t.no, order_type, t.company_name, t.store_name, t.product, t.pay_amount > t.total ? t.pay_amount : t.total, t.pay_amount, t.pay_type, t.created_at.strftime('%Y-%m-%d %H:%M:%S'), '已付款']
                row << [t.order_no, t.no, order_type, t.company_name, t.store_name, t.product, t.pay_amount > t.total ? t.pay_amount : t.total, -t.pay_amount, t.pay_type, t.updated_at.strftime('%Y-%m-%d %H:%M:%S'), '已退款']
              else
                if search_start_date.to_time <= t.created_at && t.created_at <= search_end_date.to_time
                  #下单日期在搜索范围
                  row << [t.order_no, t.no, order_type, t.company_name, t.store_name, t.product, t.pay_amount > t.total ? t.pay_amount : t.total, t.pay_amount, t.pay_type, t.created_at.strftime('%Y-%m-%d %H:%M:%S'), '已付款']
                else
                  row << [t.order_no, t.no, order_type, t.company_name, t.store_name, t.product, t.pay_amount > t.total ? t.pay_amount : t.total, -t.pay_amount, t.pay_type, t.updated_at.strftime('%Y-%m-%d %H:%M:%S'), '已退款']
                end
              end
            else
              row << [t.order_no, t.no, order_type, t.company_name, t.store_name, t.product, t.pay_amount > t.total ? t.pay_amount : t.total, t.pay_amount, t.pay_type, t.created_at.strftime('%Y-%m-%d %H:%M:%S'), '已付款']
            end
          end
        end
        bom = "\xFF\xFE".force_encoding("UTF-16LE")
        send_data (bom + csv.encode("UTF-16LE")), type: Mime[:csv],
                  disposition: "attachment; filename=微信财务流水-#{Time.now.strftime("%Y-%m-%d")}.csv"
      else
        index!
      end
    end
  end
end
